/*=============================================================================

  IO CONFIGURATION DEFINITION

  FILE: HAIO_CFG.cpp
  
  DESCRIPTION

    IO configuration
    
  EXTERNALIZED FUNCTIONS


   Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who       what, where, why
  ---------  -------  -------------------------------------------------------
   07/15/04   nso4th    Initial version

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <Cust_Target.h>
#include "HAIO_cfg.h"

/*=============================================================================
  CONSTATN DEFINITION
=============================================================================*/
#define HAIO_DEV_NUM			(1)

/*-------------------------------------------------------------------
  IO DEVICE 0
-------------------------------------------------------------------*/
static aiodev_setup_s gaioDEV0 = {
#ifdef TG_HW_AIODEV_SIMUL
	DIODEV_EMULATOR,
#else
	AIODEV_AIO, //dev_name
#endif
	"", //dev_str
	0, //subID
	16, //numIn
	16, //numOut
	{ \
		// INPUT		
		{"I00: Reserved"},
		{"I01: Reserved"},
		{"I02: Reserved"},
		{"I03: Reserved"},
			
		{"I04: Reserved"},
		{"I05: Reserved"},
		{"I06: Reserved"},
		{"I07: Reserved"},
			
		{"I08: Reserved"},
		{"I09: Reserved"},
		{"I10: Reserved"},
		{"I11: Reserved"},
			
		{"I12: Reserved"},
		{"I13: Reserved"},
		{"I14: Reserved"},
		{"I15: Reserved"},

		// OUTPUT
		{"O00: Reserved"},
		{"O01: Reserved"},
		{"O02: Reserved"},
		{"O03: Reserved"},
			
		{"O04: Reserved"},
		{"O05: Reserved"},
		{"O06: Reserved"},
		{"O07: Reserved"},

		{"O08: Reserved"},
		{"O09: Reserved"},
		{"O10: Reserved"},
		{"O11: Reserved"},
			
		{"O12: Reserved"},
		{"O13: Reserved"},
		{"O14: Reserved"},
		{"O15: Reserved"},
	} \
};

/*=============================================================================
  EXTERNALIZED FUNCION DEFINITION
=============================================================================*/

aiodev_cfg_s *aiodev_default_alloc(void)
{
	aiodev_cfg_s		*cfg;

	cfg = (aiodev_cfg_s*)memCalloc(sizeof(*cfg));

	cfg->dev_num = HAIO_DEV_NUM;
	cfg->tbl = (aiodev_setup_s*)memCalloc(sizeof(*(cfg->tbl))*(cfg->dev_num));

	memCopy((cfg->tbl+0), &gaioDEV0, sizeof(aiodev_setup_s));

	return	cfg;
}


void aiodev_default_free(aiodev_cfg_s *cfg)
{
	if(cfg == NULL)
		return;

	if(cfg->tbl)
	{
		memFree(cfg->tbl);
		cfg->tbl = NULL;
	}
	memFree(cfg);
}

#define AIO_HEADER_LEN					(10)
#define AIO_HEADER						("HAIO")
#define AIO_VER_MAJOR					(01)
#define AIO_VER_MINOR					(00)

typedef struct
{
	char			szHead[AIO_HEADER_LEN];  // "HAIOV01.00"
	aiodev_cfg_s	*cfg;
	word		wSum;  // Check sum
} aiodev_cfg_file_s;

aiodev_cfg_ptr aiodev_cfg_alloc(void)
{
	aiodev_cfg_s		*cfg;

	cfg = (aiodev_cfg_s*)memCalloc(sizeof(*cfg));

	return	cfg;
}

void aiodev_cfg_free(aiodev_cfg_s *cfg)
{
	if(cfg == NULL)
		return;

	if(cfg->tbl)
	{
		memFree(cfg->tbl);
	}
	memFree(cfg);
}

aiodev_cfg_ptr aiodev_cfg_load(char *pszname)
{
	aiodev_cfg_s		*cfg;
	mcxt			fp;
	aiodev_cfg_file_s	hf;
	byte				*buf, *p;
	int32			nLen, lr, i;
	word			wsum;
	char				szHead[AIO_HEADER_LEN+1];

	
	if((fp = FileIo_Open(pszname, FIO_OPEN_R)) == NULL)
		return	NULL;

	if((FileIo_Size(fp, (dword*)&nLen) == FALSE) || (nLen < (AIO_HEADER_LEN+2+4)))
	{
		FileIo_Close(fp);
		return	NULL;
	}

	// Read
	buf = (byte*)memCalloc(nLen);
	if((FileIo_Read(fp, buf, nLen, (dword*)&lr) == FALSE) || (nLen != lr))
	{
		memFree(buf);
		FileIo_Close(fp);
		return	NULL;
	}
	FileIo_Close(fp);

	// Compare check-sum
	p = buf + nLen - 2;
	wsum = FileIo_CheckSum((word*)buf, (nLen-2));
	if(wsum != *((word*)p))
	{
		memFree(buf);
		return	NULL;
	}

	// Read Header & Check Version
	p = buf;
	memCopy(hf.szHead, p, sizeof(hf.szHead));
	p += sizeof(hf.szHead);
	sprintf(szHead,"%sV%002d.%002d", AIO_HEADER, AIO_VER_MAJOR, AIO_VER_MINOR);
	if(strncmp(hf.szHead, szHead, AIO_HEADER_LEN) != 0)
	{
		memFree(buf);
		return	NULL;
	}

	// Get Link Information
	cfg = (aiodev_cfg_s*)memCalloc(sizeof(*cfg));
	cfg->dev_num = *((int32*)p);
	p += sizeof(cfg->dev_num);

	cfg->tbl = (aiodev_setup_s*)memCalloc(sizeof(*(cfg->tbl))*(cfg->dev_num));
	for(i=0; i<cfg->dev_num; i++)
	{
		memCopy(cfg->tbl+i, p, sizeof(*(cfg->tbl)));
		p += sizeof(*(cfg->tbl));
	}
	memFree(buf);
		
	return	cfg;
}

boolean aiodev_cfg_save(aiodev_cfg_s *cfg, char *pszname)
{
	mcxt			fp;
	byte				*buf, *p;
	int32			nLen, lw, i;
	char				szHead[AIO_HEADER_LEN+1];
	
	if((cfg == NULL) || (cfg->dev_num == 0))
		return	FALSE;

	if((fp = FileIo_Open(pszname, FIO_OPEN_WR)) == NULL)
		return	NULL;

	// Total Length
	nLen = AIO_HEADER_LEN + sizeof(cfg->dev_num) + (cfg->dev_num)*(sizeof(*(cfg->tbl))) + 2;
	buf = (byte*)memCalloc(nLen);
	p = buf;
	
	// Make Header
	sprintf(szHead,"%sV%002d.%002d", AIO_HEADER, AIO_VER_MAJOR, AIO_VER_MINOR);
	memCopy(p, szHead, AIO_HEADER_LEN);
	p += AIO_HEADER_LEN;
	
	// Config. Information
	memCopy(p, cfg, sizeof(cfg->dev_num));
	p += sizeof(cfg->dev_num);
	for(i=0; i<cfg->dev_num; i++)
	{
		memCopy(p, (cfg->tbl + i), sizeof(*(cfg->tbl)));
		p += sizeof(*(cfg->tbl));
	}

	// Get Check sum
	*((word*)(p)) = FileIo_CheckSum((word*)buf, nLen-2);

	// Save File
	if((FileIo_Write(fp, buf, nLen, (dword*)&lw) == FALSE) || (nLen != lw))
	{
		memFree(buf);
		FileIo_Close(fp);
		FileIo_DeleteFile(pszname);

		return	FALSE;
	}

	memFree(buf);
	FileIo_Close(fp);
	
	return	TRUE;
}

/*--- END OF HAIO_CFG.C ---*/
